/*
 * @Date: 2024-03-10 21:42:10
 * @LastEditors: 玄绮 shijiahansi@qq.com
 * @LastEditTime: 2024-04-13 09:31:17
 * @FilePath: \server\src\server\app\ChatSession.h
 */
#ifndef CHATSESSION_H
#define CHATSESSION_H

#pragma once
#include "AbstractSession.h"
#include "ChatParticipant.hpp"
#include "mysql/MysqlUtils.h"
// #include "packet/PacketStream.h"

// #include <data/Account.h>
// #include <data/ChatRoomInfo.h>
// #include <packet/file/FilePacket.h>
// #include <packet/file/FilePacketEx.h>
// #include <packet/request/Request.h>
// #include <packet/response/Response.h>
#include <boost/enable_shared_from_this.hpp>
#include <data/GroupInfo.pb.h>
#include <data/Message.pb.h>
#include <data/UserInfo.pb.h>
#include <memory>
#include <method/Method.pb.h>
#include <method/Request.pb.h>
#include <method/Response.pb.h>
#include <packge/packge.h>

using namespace proto;

class ChatSession : public AbstractSession,
                    public MysqlUtils,
                    public ChatParticipant,
                    public boost::enable_shared_from_this<ChatSession> {
protected:
    boost::asio::io_context& io_context;
    tcp::socket socket;

    // 验证码相关使用
    bool isVerifyVerificationCode = false;                         // 验证码是否验证成功
    int verficatonCode            = 0;                             // 验证码
    std::chrono::system_clock::time_point verficationCodeTimeout;  // 验证码超时时间

public:
    using SharedPtr = boost::shared_ptr<ChatSession>;
    ChatSession(boost::asio::io_context& io_context, tcp::socket socket);
    ~ChatSession();

protected:
    void async_read();
    void sendPacket(packge::Packge::shared_ptr pack);
    void sendPacket(method::Response& res);
    void sendPacket(std::shared_ptr<std::string> buff);

    void processPacket(packge::Packge::shared_ptr pack);
    void processRequestPacket(packge::Packge::shared_ptr pack);
    void processMessagePacket(packge::Packge::shared_ptr pack);
    void processFilePacket(packge::Packge::shared_ptr pack);

protected:
    void onUserLogin(data::Account accont);
    void onRegisterUser(method::Request& req);
    void onGetFriendsList();
    void onGetRoomList();
    void onSetAccount(method::Request& req);
    void onSetUserInfo(method::Request& req);
    void onGetUserInfo(method::Request& req);
    void onJoinRoom(method::Request& req);
    void onGetFile(std::string url);
    void onGetFileEx(std::string url);
    void onCreateGroup(data::ChatRoomInfo& roomInfo);
    void onGetRoomMembers(std::string roomId);
    void onSearchAuthor(std::string text);
    void onSearchGroup(std::string text);
    void onAddFriends(std::string userid);
    void onDelFriends(std::string userid);
    void onSendVerificationCode(std::string bind);
    void onVerifyVerificationCode(std::string code);

protected:
    bool isLogin();
    bool login();
    void unlogin();
    void initConnect();

    // bool saveFile(packet::file::FilePacket& pack);
    // bool loadFile(packet::file::FilePacket& pack);

public:
    virtual void start() override;
    virtual void deliver(data::Message msg) override;

    void slotOnRoomNewMessage(data::Message msg);

private:
};

#endif